<template>
  <div class="course-center">
    <div class="theme-detail-page">
      <!-- 页面头部 -->
      <div class="back-button">
        <el-button type="primary" class="create-btn" @click="handleBack">
          <el-icon>
            <ArrowLeft />
          </el-icon>
          返回主题列表
        </el-button>
      </div>
      <div class="theme-header">
        <div class="header-content">
          <h1 class="theme-title">{{ themeData.label }}</h1>
          <p class="theme-description">{{ themeData.description }}</p>
        </div>
        <div class="header-cover">
          <img
              v-if="themeData.coverImage"
              :src="themeData.coverImage"
              :alt="themeData.name"
              @error="handleImageError"
          >
        </div>
      </div>

      <!-- 子主题列表 -->
      <div class="sub-themes-section">
        <h2 class="section-title">相关课程</h2>
        <div class="sub-themes-grid">
          <div
              v-for="subTheme in themeData.subThemes"
              :key="subTheme.id"
              class="sub-theme-card"
          >
            <!-- 图片 -->
            <div class="card-image">
              <img
                  :src="subTheme.coverImage || '/images/default-course.jpg'"
                  :alt="subTheme.name"
                  @error="handleImageError"
              >
            </div>

            <!-- 内容 -->
            <div class="card-content">
              <h3 class="sub-theme-title">{{ subTheme.name }}</h3>
              <p class="sub-theme-description">
                {{ subTheme.description || '暂无描述' }}
              </p>

              <div class="card-meta">
                <span class="school-info">XXX学院</span>

                <div class="stats">
                <span class="stat-item">
                  <el-icon><View /></el-icon>
                  {{ Math.floor(Math.random() * 1000) + 100 }} 浏览
                </span>
                  <span class="stat-item">
                  <el-icon><Star /></el-icon>
                  {{ Math.floor(Math.random() * 500) + 50 }} 点赞
                </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-if="themeData.subThemes && themeData.subThemes.length === 0" class="empty-state">
        <el-empty description="暂无子主题内容" />
      </div>

      <!-- 加载状态 -->
      <div v-if="loading" class="loading-state">
        <el-skeleton :rows="5" animated />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute,useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import {View, Star, Plus} from '@element-plus/icons-vue'
import { getThemeWithAllSubThemes } from '@/api/teacher'

const route = useRoute()
const router = useRouter()
const themeId = ref(null)
const themeData = ref({
  id: null,
  name: '',
  description: '',
  coverImage: '',
  subThemes: []
})
const loading = ref(false)

// 获取主题详情
const fetchThemeDetail = async () => {
  if (!themeId.value) return

  loading.value = true
  try {
    const response = await getThemeWithAllSubThemes(themeId.value)
    if (response.success) {
      themeData.value = response.data
    } else {
      ElMessage.error(response.message || '获取主题详情失败')
    }
  } catch (error) {
    console.error('获取主题详情失败:', error)
    ElMessage.error('获取主题详情失败，请重试')
  } finally {
    loading.value = false
  }
}

// 图片加载失败处理
const handleImageError = (event) => {
  event.target.src = ''
}

onMounted(() => {
  themeId.value = Number(route.params.id)
  if (themeId.value) {
    fetchThemeDetail()
  } else {
    ElMessage.error('主题ID不存在')
  }
})

const handleBack = () => {
  router.back() // 返回上一页
  // 或者直接跳转到指定页面：
  // router.push('/themes')
}
</script>

<style scoped>
.course-center {
  padding: 20px;
  //background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.theme-detail-page {
  //max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

/* 页面头部 */
.theme-header {
  display: flex;
  gap: 40px;
  margin-bottom: 40px;
  padding: 30px;
  background: #fff;
  border-radius: 12px;
  //color: #333;
}

.header-content {
  flex: 1;
}

.theme-title {
  font-size: 32px;
  font-weight: 700;
  margin-bottom: 16px;
  //color: white;
}

.theme-description {
  font-size: 16px;
  line-height: 1.6;
  opacity: 0.9;
  margin: 0;
}

.header-cover {
  width: 300px;
  height: 200px;
  border-radius: 8px;
  overflow: hidden;
}

.header-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
/* 返回按钮 */
.back-button {
  margin-bottom: 20px;
}

.back-btn {
  color: #f56c6c !important;
  font-weight: 500;
  padding: 8px 10px;
  background:white;
}

.back-btn:hover {
  color: #f78989 !important;
}

.back-btn .el-icon {
  margin-right: 6px;
}

/* 子主题区域 */
.section-title {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 24px;
  color: #1f2d3d;
  border-left: 4px solid #409eff;
  padding-left: 12px;
}

.sub-themes-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
  margin-bottom: 40px;
}

/* 子主题卡片 */
.sub-theme-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
}

.sub-theme-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.card-image {
  width: 100%;
  height: 160px;
  overflow: hidden;
}

.card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.sub-theme-card:hover .card-image img {
  transform: scale(1.05);
}

.card-content {
  padding: 20px;
}

.sub-theme-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #1f2d3d;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.sub-theme-description {
  font-size: 14px;
  color: #606266;
  line-height: 1.5;
  margin-bottom: 16px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  min-height: 42px;
}

.card-meta {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.school-info {
  font-size: 13px;
  color: #909399;
  font-weight: 500;
}

.stats {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  color: #909399;
}

.stat-item .el-icon {
  font-size: 14px;
}

/* 空状态和加载状态 */
.empty-state,
.loading-state {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

/* 响应式设计 */
@media (max-width: 1200px) {
  .sub-themes-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .theme-detail-page {
    padding: 16px;
  }

  .theme-header {
    flex-direction: column;
    gap: 20px;
    text-align: center;
  }

  .header-cover {
    width: 100%;
    height: 200px;
  }

  .sub-themes-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
}

@media (max-width: 480px) {
  .sub-themes-grid {
    grid-template-columns: 1fr;
  }
}
</style>
